﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NutriLeaf.Web.Core.Meals
{
    public class Meal:IMeal
    {
        public void AddMeal(MealData Meal, ref Model.NutriLeafDBEntities context)
        {
       
           //doubt
        }

        public void AddMeal(string MealName, MealType category, string descirption, int healthRatingId, ref Model.NutriLeafDBEntities context)
        {
            Model.Meal m = new Model.Meal();
            m.MealName = MealName;
            m.MealCategory.MealCategoryID = (int)category;
            m.MealDescription = descirption;
            m.HealthRatingID = healthRatingId
                ;
            context.Meals.AddObject(m);
            context.SaveChanges();
        }

        public List<Model.Meal> SuggestMeals(int userId, DateTime clientDateTime, ref Model.NutriLeafDBEntities context)
        {
            List<Model.Meal> me = new List<Model.Meal>();
          List<Model.UserMealFav> m=  context.UserMealFavs.Where(p => p.UserID == userId).ToList();
          foreach (Model.UserMealFav meals in m) {
              me.Add(meals.Meal);
          }
          return me;
        }

        public void RemoveMeal(int MealId, ref Model.NutriLeafDBEntities context)
        {
           context.DeleteObject(context.Meals.Where(p => p.MealID == MealId).First());
        }

        public void UpdateMeal(MealData MealData, ref Model.NutriLeafDBEntities context)
        {
            Model.Meal m = context.Meals.Where(p => p.MealID==MealData.meal.MealID).First();
            m.MealCategory.MealCategoryID = (int)MealData.category;
            foreach( Model.Food food in MealData.foods.ToList()){
                Model.FoodInMeal foodmeal = new Model.FoodInMeal();
               foodmeal.Meal = m;
                foodmeal.Food = food;
                context.FoodInMeals.AddObject(foodmeal);
                
            }
            context.SaveChanges();
   
        }

        public void UpdateMeal(Model.Meal Meal, ref Model.NutriLeafDBEntities context)
        {
            Model.Meal m = context.Meals.Where(p => p.MealID == Meal.MealID).First();
            m = Meal;
            context.SaveChanges();
        }
    }
}